package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/checkgroup")
@RestController
public class CheckgroupController {
    @Autowired
    private CheckgroupService checkgroupService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = checkgroupService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result select(Integer id){
        CheckGroup checkGroup = checkgroupService.selectById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS,checkGroup);
    }

    /**
     * 根据检查组id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        List<String> itemList = checkgroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS,itemList);
    }

    /**
     * 编辑检查项
     * @param checkitemIds
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        checkgroupService.edit(checkGroup,checkitemIds);
        return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        checkgroupService.insert(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id删除检查组
     * @param id
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result delete(Integer id){
        checkgroupService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("[检查项-查询所有]~");
        List<CheckGroup> checkGroups = checkgroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkGroups);
    }
}
